package com.qfedu.dao.impl;

import com.qfedu.dao.OrderDao;
import com.qfedu.pojo.Order;
import com.qfedu.utils.DataSourceUtils;
import com.qfedu.utils.TxUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

/**
 * 创建者: QF-SUN
 * 创建时间: 2023/4/6 10:22
 * 描述:
 */
public class OrderDaoImpl implements OrderDao {
    QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());


    @Override
    public long findCountByAddId(String id) {
        String sql = "select count(0) from tb_order where aid = ?";
        try {
            long count = (long) qr.query(sql, new ScalarHandler(), id);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void insert(Order order) {
        QueryRunner qr = new QueryRunner();
        String sql = "INSERT INTO `tb_order`(`id`,`uid`,`money`,`status`,`time`,`aid`)VALUES(?,?,?,?,?,?);";
        try {
            qr.update(TxUtils.getConn(),sql, order.getId(), order.getUid(), order.getMoney(), order.getStatus(), order.getTime(),order.getAid());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("新增订单失败");
        }
    }
}
